package com.xiaoq.matrix.minipro.endpoint;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.xiaoq.coms.util.ClassPathResourceUtil;


@RestController
@RequestMapping("/cgi-bin/component")
public class WechatMockServerEndpointA {
	
	private void responseFromFile(HttpServletResponse resp, String path) {
		InputStream ins = null;
		try {
			ins = ClassPathResourceUtil.getInputStream(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			resp.setCharacterEncoding("UTF-8");
			List<String> lines = IOUtils.readLines(ins, "UTF-8");
			for (String line : lines) {
				resp.getWriter().println(line);
				System.out.println(line);
			}
			resp.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		IOUtils.closeQuietly(ins);
	}
	
	@PostMapping(value = "/ticket", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void ticket(HttpServletResponse resp) throws Exception {
		RestTemplate rest = new RestTemplate();
		rest.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);
		InputStream ins = ClassPathResourceUtil.getInputStream("/weapp/mock/ticket.xml");
		String xml = IOUtils.toString(ins, "UTF-8");
		IOUtils.closeQuietly(ins);
		HttpEntity<String> reqEntity = new HttpEntity<String>(xml, headers);
		String respStr = null;
		try {
			respStr = rest.postForObject(new URI("http://localhost:9702/api/v1/weixin/minip/ticket"),
					reqEntity,
					String.class);
		} catch (RestClientException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		resp.getWriter().println(respStr);
	}

	@PostMapping(value = "/event", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void event(HttpServletResponse resp, @RequestParam Boolean isSuccess) throws Exception {
		RestTemplate rest = new RestTemplate();
		rest.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_XML);
		headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);
		InputStream ins = ClassPathResourceUtil.getInputStream(isSuccess ? "/weapp/mock/audit_success_event.xml" : "/weapp/mock/audit_fail_event.xml");
		String xml = IOUtils.toString(ins, "UTF-8");
		IOUtils.closeQuietly(ins);
		HttpEntity<String> reqEntity = new HttpEntity<String>(xml, headers);
		String respStr = null;
		try {
			respStr = rest.postForObject(new URI("http://localhost:9702/api/v1/weixin/minip/event"),
					reqEntity,
					String.class);
		} catch (RestClientException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		resp.getWriter().println(respStr);
	}

	
	@PostMapping(value = "/api_component_token", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void componentToken(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/component_token.json");
	}
	
	@PostMapping(value = "/api_create_preauthcode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void createPreAuthCode(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/pre_auth_code.json");
	}
	
	@PostMapping(value = "/api_query_auth", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void getAuthorizerInfo(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/authorizer_info.json");
	}
	
}
